import 'dart:io';

import 'package:file_picker/file_picker.dart';
import 'package:flutter/material.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter_spinkit/flutter_spinkit.dart';
import '../../helper/Helper.dart';
import '../../helper/global_config.dart';
import '../../pages/things/things_info.dart';
import '../../model/things.dart';
import '../../helper/routeHelper.dart';
import '../../helper/uidata.dart';
import '../../model/skddl.dart';
import '../skddl/sdddl_authorty.dart';
import '../../widgets/someCards.dart';

class ThingsListPage extends StatefulWidget {
  final bool ifAppbar;
  @override
  ThingsListPage(this.ifAppbar);
  @override
  ThingsListPageState createState() => new ThingsListPageState();
}

class ThingsListPageState extends State<ThingsListPage> {
  List<Things> RList = <Things>[];
  bool isEnd = false;
  ScrollController _scrollController = new ScrollController();
  bool firstLoading = false;
  int canShow = 0;

  @override
  void initState() {
    // TODO: implement initState
    super.initState();

    getRefrush();
  }

  Future<Null> delete(Things r1) async {
    // await addOne();
    await Things.Delete(r1);
    setState(() {
      RList.remove(r1);
    });
  }

  onDeleted(Things r1) => {
        setState(() {
          RList.remove(r1);
        })
      };

  Future<Null> getRefrush() async {
    var list1 = await Things.GetAllList();
    setState(() {
      RList = list1;
    });
  }

  Widget thingsCard(Things x1) {
    return Card(
        key: Key(x1.ID.toString()),
        margin: const EdgeInsets.only(top: 0.2, bottom: 0.2),
        color: GlobalConfig.cardBackgroundColor,
        shape: const RoundedRectangleBorder(borderRadius: BorderRadius.zero),
        child: ListTile(
          //leading: ImageWidget.StationImage(StationList[index].Name),
          title: Text(x1.TName),
          //trailing: Text(StationList[index].STCD),
          onTap: () {
            RouteHelper.ShowPage(context, ThingsInfoPage(x1));
          },
          onLongPress: () {
            RouteHelper.ThingsAdd(context, x1, getRefrush);
          },
        ));
  }

  @override
  Widget build(BuildContext context) {
    if (RList == null) return new Container();
    // TODO: implement build
    return new Scaffold(
        appBar: widget.ifAppbar
            ? AppBar(
                title: Text('物品列表'),
                actions: <Widget>[
                  IconButton(
                      icon: Icon(
                        Icons.add,
                        color: Colors.white,
                      ),
                      onPressed: () {
                        RouteHelper.ThingsAdd(context, null, getRefrush);
                      }),
                ],
              )
            : null,
        floatingActionButton: FloatingActionButton(
          child: Icon(Icons.refresh),
          onPressed: () => getRefrush(),
        ),
        body: firstLoading == true
            ? Center(
                child: SpinKitCircle(
                color: Colors.blue,
              ))
            : Container(
                margin: EdgeInsets.only(top: 8.0),
                child: new RefreshIndicator(
                  child: ListView.builder(
                    itemCount: RList.length + 1,
                    controller: _scrollController,
                    physics: const AlwaysScrollableScrollPhysics(),
                    itemBuilder: (BuildContext context, int index) {
                      if (index != RList.length) {
                        return thingsCard(RList[index]);
                      } else {
                        if (isEnd == true) {
                          return Container(
                              margin: EdgeInsets.all(12.0),
                              child: Center(
                                  child: Text(
                                "没有更多了",
                                style: TextStyle(color: Colors.grey),
                              )));
                        } else {
                          return new Container();
                        }
                      }
                    },
                  ),
                  onRefresh: getRefrush,
                )));
  }
}
